package com.example.his.api.mis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.his.api.mis.service.UserService;
import com.example.his.api.mis.controller.form.LoginForm;
import com.example.his.api.mis.controller.form.SearchUserByPageForm;
import com.example.his.api.mis.controller.form.UpdatePasswordForm;
import com.example.his.api.db.dao.UserDao;
import com.example.his.api.db.pojo.UserEntity;
import com.example.his.api.utils.PageUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity>  implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public Integer login(LoginForm form) {
            //password MD5加密
//        MD5 md5 = MD5.create();
//        String temp = md5.digestHex(username);
//        String tempStart = StrUtil.subWithLength(temp, 0, 6);
//        String tempEnd = StrUtil.subSuf(temp, temp.length() - 3);
//        password = md5.digestHex(tempStart + password + tempEnd).toUpperCase();
//        param.replace("password", password);
        UserEntity one = this.getOne(new LambdaQueryWrapper<UserEntity>()
                .select(UserEntity::getId)
                .eq(UserEntity::getUsername, form.getUsername())
                .eq(UserEntity::getPassword, form.getPassword())
                .last("limit 0,1")
        );

        return one == null ? null :  one.getId();
    }

    @Override
    public Boolean updatePassword(UpdatePasswordForm form) {
        return  this.update(new LambdaUpdateWrapper<UserEntity>()
                .eq(UserEntity::getId, form.getUserId())
                .eq(UserEntity::getPassword, form.getPassword())
                .set(UserEntity::getPassword, form.getNewPassword()));
    }

    @Override
    public PageUtils searchByPage(SearchUserByPageForm form) {
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Page<List<HashMap<String,Objects>>> page = PageHelper.startPage(form.getPage(), form.getLength()).doSelectPage(() -> userDao.searchByPage(map));
        return new PageUtils(page.getResult(),page.getTotal(), form.getPage(), form.getLength());
    }

    @Override
    public Boolean insert(UserEntity user) {
        return this.save(user);
    }

    @Override
    public UserEntity searchById(Integer id) {
        return this.getById(id);
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
       return baseMapper.delete(new LambdaQueryWrapper<UserEntity>()
               .in(UserEntity::getId,ids)
               .eq(UserEntity::getRoot,false));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int dismiss(Integer id,Integer status) {
        UpdateWrapper<UserEntity> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq("root",false);
        wrapper.set("status",status);
        return baseMapper.update(null, wrapper);
    }

    @Override
    public HashMap searchDoctorById(int userId) {
        HashMap map = userDao.searchDoctorById(userId);
        return map;
    }


}
